//package com.springboot.study.tests.netty;
//
//import io.netty.bootstrap.ServerBootstrap;
//import io.netty.channel.ChannelFuture;
//import io.netty.channel.ChannelInitializer;
//import io.netty.channel.ChannelOption;
//import io.netty.channel.EventLoopGroup;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.SocketChannel;
//import io.netty.channel.socket.nio.NioServerSocketChannel;
//import io.netty.channel.socket.nio.NioSocketChannel;
//import io.netty.handler.codec.LineBasedFrameDecoder;
//import io.netty.handler.codec.string.StringDecoder;
//
//import java.net.ServerSocket;
//
///**
// * @Author: guodong
// * @Date: 2020/12/14 14:04
// * @Version: 1.0
// * @Description:
// */
//public class TimeServer {
//
//
//    public void bind(int port) throws Exception{
//        // 配置服务端的NIO线程组
//        EventLoopGroup bossGroup = new NioEventLoopGroup();
//        EventLoopGroup workGroup = new NioEventLoopGroup();
//
//        try {
//            ServerBootstrap serverBootstrap = new ServerBootstrap();
//            serverBootstrap.group(bossGroup, workGroup).channel(NioServerSocketChannel.class)
//                    .option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler());
//
//            // 绑定端口 同步等待成功
//            ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
//            // 等待服务端监听端口关闭
//            channelFuture.channel().closeFuture().sync();
//        } finally {
//            // 优雅退出 释放线程池资源
//            bossGroup.shutdownGracefully();
//            workGroup.shutdownGracefully();
//        }
//
//    }
//
//
//    /**
//     * LineBasedFrameDecoder StringDecoder 两个解码器 解决tcp的粘包
//     */
//    private class ChildChannelHandler extends ChannelInitializer<SocketChannel>{
//        @Override
//        protected void initChannel(SocketChannel socketChannel) throws Exception {
//            socketChannel.pipeline().addLast(new LineBasedFrameDecoder(1024));
//            socketChannel.pipeline().addLast(new StringDecoder());
//            socketChannel.pipeline().addLast(new TimeServerHandler());
//        }
//    }
//
//
//    public static void main(String[] args) throws Exception{
//        int port = 8080;
//        new TimeServer().bind(port);
//    }
//
//
//
//
//}
